{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-1 面部检测\n",
    "import requests\n",
    "import json\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = 'aa3f3860bc894397a5018c908cbb79ff'\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-hr.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文body\n",
    "image_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603820202732&di=86272f613bbc0793a37c8afcabc088df&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202007%2F30%2F20200730175754_rhdsN.thumb.200_200_c.jpeg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数parameters\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->bytes\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# bytes or string   编码不同  *****  encoding =\"utf-8\"\n",
    "# response.text\n",
    "# response.content\n",
    "results = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'bf345c8a-376d-473a-8f57-75e0699ea576',\n",
       "  'faceRectangle': {'top': 40, 'left': 74, 'width': 58, 'height': 58},\n",
       "  'faceAttributes': {'smile': 0.604,\n",
       "   'headPose': {'pitch': 0.7, 'roll': 4.8, 'yaw': 14.9},\n",
       "   'gender': 'female',\n",
       "   'age': 23.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.003,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.604,\n",
       "    'neutral': 0.392,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.75},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.4},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.09,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.9},\n",
       "     {'color': 'red', 'confidence': 0.85},\n",
       "     {'color': 'other', 'confidence': 0.54},\n",
       "     {'color': 'black', 'confidence': 0.42},\n",
       "     {'color': 'blond', 'confidence': 0.31},\n",
       "     {'color': 'gray', 'confidence': 0.05},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'bf345c8a-376d-473a-8f57-75e0699ea576',\n",
       "  'faceRectangle': {'top': 40, 'left': 74, 'width': 58, 'height': 58},\n",
       "  'faceAttributes': {'smile': 0.604,\n",
       "   'headPose': {'pitch': 0.7, 'roll': 4.8, 'yaw': 14.9},\n",
       "   'gender': 'female',\n",
       "   'age': 23.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.003,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.604,\n",
       "    'neutral': 0.392,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.0},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.75},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.4},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.09,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.9},\n",
       "     {'color': 'red', 'confidence': 0.85},\n",
       "     {'color': 'other', 'confidence': 0.54},\n",
       "     {'color': 'black', 'confidence': 0.42},\n",
       "     {'color': 'blond', 'confidence': 0.31},\n",
       "     {'color': 'gray', 'confidence': 0.05},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-2 json转译\n",
    "results = response.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceAttributes.smile</th>\n",
       "      <th>faceAttributes.headPose.pitch</th>\n",
       "      <th>faceAttributes.headPose.roll</th>\n",
       "      <th>faceAttributes.headPose.yaw</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>...</th>\n",
       "      <th>faceAttributes.noise.value</th>\n",
       "      <th>faceAttributes.makeup.eyeMakeup</th>\n",
       "      <th>faceAttributes.makeup.lipMakeup</th>\n",
       "      <th>faceAttributes.accessories</th>\n",
       "      <th>faceAttributes.occlusion.foreheadOccluded</th>\n",
       "      <th>faceAttributes.occlusion.eyeOccluded</th>\n",
       "      <th>faceAttributes.occlusion.mouthOccluded</th>\n",
       "      <th>faceAttributes.hair.bald</th>\n",
       "      <th>faceAttributes.hair.invisible</th>\n",
       "      <th>faceAttributes.hair.hairColor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>bf345c8a-376d-473a-8f57-75e0699ea576</td>\n",
       "      <td>40</td>\n",
       "      <td>74</td>\n",
       "      <td>58</td>\n",
       "      <td>58</td>\n",
       "      <td>0.604</td>\n",
       "      <td>0.7</td>\n",
       "      <td>4.8</td>\n",
       "      <td>14.9</td>\n",
       "      <td>female</td>\n",
       "      <td>...</td>\n",
       "      <td>0.4</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>[]</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>0.09</td>\n",
       "      <td>False</td>\n",
       "      <td>[{'color': 'brown', 'confidence': 0.9}, {'colo...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId  faceRectangle.top  \\\n",
       "0  bf345c8a-376d-473a-8f57-75e0699ea576                 40   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.width  faceRectangle.height  \\\n",
       "0                  74                   58                    58   \n",
       "\n",
       "   faceAttributes.smile  faceAttributes.headPose.pitch  \\\n",
       "0                 0.604                            0.7   \n",
       "\n",
       "   faceAttributes.headPose.roll  faceAttributes.headPose.yaw  \\\n",
       "0                           4.8                         14.9   \n",
       "\n",
       "  faceAttributes.gender  ...  faceAttributes.noise.value  \\\n",
       "0                female  ...                         0.4   \n",
       "\n",
       "   faceAttributes.makeup.eyeMakeup  faceAttributes.makeup.lipMakeup  \\\n",
       "0                             True                             True   \n",
       "\n",
       "   faceAttributes.accessories faceAttributes.occlusion.foreheadOccluded  \\\n",
       "0                          []                                     False   \n",
       "\n",
       "   faceAttributes.occlusion.eyeOccluded  \\\n",
       "0                                 False   \n",
       "\n",
       "   faceAttributes.occlusion.mouthOccluded  faceAttributes.hair.bald  \\\n",
       "0                                   False                      0.09   \n",
       "\n",
       "   faceAttributes.hair.invisible  \\\n",
       "0                          False   \n",
       "\n",
       "                       faceAttributes.hair.hairColor  \n",
       "0  [{'color': 'brown', 'confidence': 0.9}, {'colo...  \n",
       "\n",
       "[1 rows x 38 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-3 pandas数据表格化\n",
    "import pandas as pd\n",
    "df_face = pd.json_normalize(results)\n",
    "df_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['bf345c8a-376d-473a-8f57-75e0699ea576']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-4 数据取值\n",
    "faceID = df_face['faceId'].values.tolist()\n",
    "faceID "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['faceId', 'faceRectangle.top', 'faceRectangle.left',\n",
       "       'faceRectangle.width', 'faceRectangle.height', 'faceAttributes.smile',\n",
       "       'faceAttributes.headPose.pitch', 'faceAttributes.headPose.roll',\n",
       "       'faceAttributes.headPose.yaw', 'faceAttributes.gender',\n",
       "       'faceAttributes.age', 'faceAttributes.facialHair.moustache',\n",
       "       'faceAttributes.facialHair.beard',\n",
       "       'faceAttributes.facialHair.sideburns', 'faceAttributes.glasses',\n",
       "       'faceAttributes.emotion.anger', 'faceAttributes.emotion.contempt',\n",
       "       'faceAttributes.emotion.disgust', 'faceAttributes.emotion.fear',\n",
       "       'faceAttributes.emotion.happiness', 'faceAttributes.emotion.neutral',\n",
       "       'faceAttributes.emotion.sadness', 'faceAttributes.emotion.surprise',\n",
       "       'faceAttributes.blur.blurLevel', 'faceAttributes.blur.value',\n",
       "       'faceAttributes.exposure.exposureLevel',\n",
       "       'faceAttributes.exposure.value', 'faceAttributes.noise.noiseLevel',\n",
       "       'faceAttributes.noise.value', 'faceAttributes.makeup.eyeMakeup',\n",
       "       'faceAttributes.makeup.lipMakeup', 'faceAttributes.accessories',\n",
       "       'faceAttributes.occlusion.foreheadOccluded',\n",
       "       'faceAttributes.occlusion.eyeOccluded',\n",
       "       'faceAttributes.occlusion.mouthOccluded', 'faceAttributes.hair.bald',\n",
       "       'faceAttributes.hair.invisible', 'faceAttributes.hair.hairColor'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查属性/特征值\n",
    "df_face.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>bf345c8a-376d-473a-8f57-75e0699ea576</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.392</td>\n",
       "      <td>23.0</td>\n",
       "      <td>female</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId faceAttributes.glasses  \\\n",
       "0  bf345c8a-376d-473a-8f57-75e0699ea576              NoGlasses   \n",
       "\n",
       "   faceAttributes.emotion.neutral  faceAttributes.age faceAttributes.gender  \n",
       "0                           0.392                23.0                female  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可观察其中几组数据\n",
    "df_face[['faceId','faceAttributes.glasses','faceAttributes.emotion.neutral','faceAttributes.age','faceAttributes.gender']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "# 1.创建人脸列表\n",
    "faceListId = \"list_004\" # 设置人脸列表ID\n",
    "create_facelists_url =\"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}\" # URL（见API文档）\n",
    "subscription_key =\"aa3f3860bc894397a5018c908cbb79ff\" # 密钥\n",
    "# 请求头\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 相关信息及识别模型的选用\n",
    "data = {\n",
    "  \"name\": \"sample_list\",\n",
    "  \"userData\": \"相册\",\n",
    "  \"recognitionModel\": \"recognition_03\"\n",
    "}\n",
    "# 发送请求\n",
    "r_create = requests.put(create_facelists_url.format(faceListId), headers=headers, json=data)\n",
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.添加人脸\n",
    "add_face_url =\"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 人脸相片地址\n",
    "img_url =\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=781918665,2083273991&fm=26&gp=0.jpg\"\n",
    "# 人脸数据\n",
    "params_add_face={\n",
    "    \"userData\":\"joy\"\n",
    "}\n",
    "# 发送请求\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '33bc32bc-2ffe-486f-9ee8-a86837911f50'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json() # 返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code #返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg\",\"丘天惠\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg\",\"林嘉茵\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg\",\"汤玲萍\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg\",\"曾雯燕\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg\",\"谢依希\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/YuecongYang.png\",\"杨悦聪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg\",\"周雨\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg\",\"刘瑜鹏\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg\",\"陈嘉仪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/kg2000.jpg\",\"徐旖芊\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg\",\"刘心如\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuyu19.png\",\"刘宇\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/ltco.jpg\",\"李婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg\",\"黄智毅\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg\",\"黄慧文\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg\",\"张铭睿\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/yichenting.jpg\",\"陈婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/coco022.jpg\",\"洪可凡\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lujizhi.png\",\"卢继志\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg\",\"张梓乐\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'e5a9b7a6-8d5e-4b81-829c-1ba94a853e52',\n",
       "  'faceRectangle': {'top': 67, 'left': 67, 'width': 95, 'height': 95},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': -11.7, 'roll': 2.6, 'yaw': -2.1},\n",
       "   'gender': 'female',\n",
       "   'age': 21.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 0.999,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.1},\n",
       "   'exposure': {'exposureLevel': 'overExposure', 'value': 0.82},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.4},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.06,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'red', 'confidence': 1.0},\n",
       "     {'color': 'other', 'confidence': 0.99},\n",
       "     {'color': 'blond', 'confidence': 0.17},\n",
       "     {'color': 'gray', 'confidence': 0.08},\n",
       "     {'color': 'brown', 'confidence': 0.02},\n",
       "     {'color': 'black', 'confidence': 0.02},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.检测人脸\n",
    "face_api_url = 'https://api-hjq.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "image_url = 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=781918665,2083273991&fm=26&gp=0.jpg'\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择模型\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数，请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "response = requests.post(face_api_url, params=params,headers=headers, json={\"url\": image_url})\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '33bc32bc-2ffe-486f-9ee8-a86837911f50',\n",
       "  'confidence': 1.0},\n",
       " {'persistedFaceId': 'de0d8858-0c64-4cba-af2b-86423ab5fad2',\n",
       "  'confidence': 0.27114},\n",
       " {'persistedFaceId': '6f06efe2-95e8-4006-8ee6-73ca74671476',\n",
       "  'confidence': 0.2688},\n",
       " {'persistedFaceId': '9d1b7f44-de9d-4f96-86d4-7713aa75e2e6',\n",
       "  'confidence': 0.23295},\n",
       " {'persistedFaceId': '9348c1b8-ccb5-4957-b77d-d00dfaefde5c',\n",
       "  'confidence': 0.14337},\n",
       " {'persistedFaceId': 'f4cbb06b-01a8-4a37-8d28-209c704d9c69',\n",
       "  'confidence': 0.10999},\n",
       " {'persistedFaceId': '6967f8ed-5236-48b2-afb9-a0695537f1c6',\n",
       "  'confidence': 0.10185},\n",
       " {'persistedFaceId': '128dd6c2-6a37-4bf7-901d-3b618f519c48',\n",
       "  'confidence': 0.09966},\n",
       " {'persistedFaceId': 'dfdbe8e3-9d76-45a7-a138-29599749d40b',\n",
       "  'confidence': 0.09824},\n",
       " {'persistedFaceId': '0c6b50b4-2cdf-4509-a0c0-b9299e4ca303',\n",
       "  'confidence': 0.09818}]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.返回人脸相似置信度\n",
    "findsimilars_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "# 请求正文 faceID需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"e5a9b7a6-8d5e-4b81-829c-1ba94a853e52\", #取上方的faceID\n",
    "    \"faceListId\": \"list_004\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\" #matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)\n",
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '33bc32bc-2ffe-486f-9ee8-a86837911f50',\n",
       "   'userData': 'joy'},\n",
       "  {'persistedFaceId': '80a0b63e-57fb-49c7-9d1c-e096504fffa1',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': 'dfdbe8e3-9d76-45a7-a138-29599749d40b',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': '8ce440e7-74a6-4543-a44c-d7280f7d178c',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': '6f06efe2-95e8-4006-8ee6-73ca74671476',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': '9348c1b8-ccb5-4957-b77d-d00dfaefde5c',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '6967f8ed-5236-48b2-afb9-a0695537f1c6',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': '128dd6c2-6a37-4bf7-901d-3b618f519c48',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': 'c2d8ddba-176a-4f88-b8f9-6f7edf792daf',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': '412eb833-1dff-4c5c-b3db-fd59eac8025f',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': 'de0d8858-0c64-4cba-af2b-86423ab5fad2',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': 'b94d8226-2aa0-4ded-bfa3-20c9a3881e94',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': 'b8253c08-2a41-4815-9643-c6941a5d2898',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': '0c6b50b4-2cdf-4509-a0c0-b9299e4ca303',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': '82dbaa20-07f9-4ff5-ae63-d5267cb5956c',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': '9d1b7f44-de9d-4f96-86d4-7713aa75e2e6',\n",
       "   'userData': '张铭睿'},\n",
       "  {'persistedFaceId': '058051f4-9fe2-4590-a486-8385c2cc587b',\n",
       "   'userData': '陈婷'},\n",
       "  {'persistedFaceId': 'f4cbb06b-01a8-4a37-8d28-209c704d9c69',\n",
       "   'userData': '洪可凡'},\n",
       "  {'persistedFaceId': '0f4d8694-4184-4eb7-994a-0aae946c64e9',\n",
       "   'userData': '卢继志'},\n",
       "  {'persistedFaceId': '23d44cb1-caff-4ec9-b7f7-5e819882418c',\n",
       "   'userData': '张梓乐'}],\n",
       " 'faceListId': 'list_004',\n",
       " 'name': 'sample_list',\n",
       " 'userData': '相册'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看列表\n",
    "get_facelist_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>33bc32bc-2ffe-486f-9ee8-a86837911f50</td>\n",
       "      <td>joy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>80a0b63e-57fb-49c7-9d1c-e096504fffa1</td>\n",
       "      <td>丘天惠</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>dfdbe8e3-9d76-45a7-a138-29599749d40b</td>\n",
       "      <td>林嘉茵</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8ce440e7-74a6-4543-a44c-d7280f7d178c</td>\n",
       "      <td>汤玲萍</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6f06efe2-95e8-4006-8ee6-73ca74671476</td>\n",
       "      <td>曾雯燕</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>9348c1b8-ccb5-4957-b77d-d00dfaefde5c</td>\n",
       "      <td>杨悦聪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6967f8ed-5236-48b2-afb9-a0695537f1c6</td>\n",
       "      <td>周雨</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>128dd6c2-6a37-4bf7-901d-3b618f519c48</td>\n",
       "      <td>刘瑜鹏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>c2d8ddba-176a-4f88-b8f9-6f7edf792daf</td>\n",
       "      <td>陈嘉仪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>412eb833-1dff-4c5c-b3db-fd59eac8025f</td>\n",
       "      <td>徐旖芊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>de0d8858-0c64-4cba-af2b-86423ab5fad2</td>\n",
       "      <td>刘心如</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>b94d8226-2aa0-4ded-bfa3-20c9a3881e94</td>\n",
       "      <td>刘宇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>b8253c08-2a41-4815-9643-c6941a5d2898</td>\n",
       "      <td>李婷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0c6b50b4-2cdf-4509-a0c0-b9299e4ca303</td>\n",
       "      <td>黄智毅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>82dbaa20-07f9-4ff5-ae63-d5267cb5956c</td>\n",
       "      <td>黄慧文</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>9d1b7f44-de9d-4f96-86d4-7713aa75e2e6</td>\n",
       "      <td>张铭睿</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>058051f4-9fe2-4590-a486-8385c2cc587b</td>\n",
       "      <td>陈婷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>f4cbb06b-01a8-4a37-8d28-209c704d9c69</td>\n",
       "      <td>洪可凡</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0f4d8694-4184-4eb7-994a-0aae946c64e9</td>\n",
       "      <td>卢继志</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>23d44cb1-caff-4ec9-b7f7-5e819882418c</td>\n",
       "      <td>张梓乐</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         persistedFaceId userData\n",
       "0   33bc32bc-2ffe-486f-9ee8-a86837911f50      joy\n",
       "1   80a0b63e-57fb-49c7-9d1c-e096504fffa1      丘天惠\n",
       "2   dfdbe8e3-9d76-45a7-a138-29599749d40b      林嘉茵\n",
       "3   8ce440e7-74a6-4543-a44c-d7280f7d178c      汤玲萍\n",
       "4   6f06efe2-95e8-4006-8ee6-73ca74671476      曾雯燕\n",
       "5   9348c1b8-ccb5-4957-b77d-d00dfaefde5c      杨悦聪\n",
       "6   6967f8ed-5236-48b2-afb9-a0695537f1c6       周雨\n",
       "7   128dd6c2-6a37-4bf7-901d-3b618f519c48      刘瑜鹏\n",
       "8   c2d8ddba-176a-4f88-b8f9-6f7edf792daf      陈嘉仪\n",
       "9   412eb833-1dff-4c5c-b3db-fd59eac8025f      徐旖芊\n",
       "10  de0d8858-0c64-4cba-af2b-86423ab5fad2      刘心如\n",
       "11  b94d8226-2aa0-4ded-bfa3-20c9a3881e94       刘宇\n",
       "12  b8253c08-2a41-4815-9643-c6941a5d2898       李婷\n",
       "13  0c6b50b4-2cdf-4509-a0c0-b9299e4ca303      黄智毅\n",
       "14  82dbaa20-07f9-4ff5-ae63-d5267cb5956c      黄慧文\n",
       "15  9d1b7f44-de9d-4f96-86d4-7713aa75e2e6      张铭睿\n",
       "16  058051f4-9fe2-4590-a486-8385c2cc587b       陈婷\n",
       "17  f4cbb06b-01a8-4a37-8d28-209c704d9c69      洪可凡\n",
       "18  0f4d8694-4184-4eb7-994a-0aae946c64e9      卢继志\n",
       "19  23d44cb1-caff-4ec9-b7f7-5e819882418c      张梓乐"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用Pandas简化数据\n",
    "import pandas as pd\n",
    "# 返回facelist数据\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>33bc32bc-2ffe-486f-9ee8-a86837911f50</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>de0d8858-0c64-4cba-af2b-86423ab5fad2</td>\n",
       "      <td>0.27114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6f06efe2-95e8-4006-8ee6-73ca74671476</td>\n",
       "      <td>0.26880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9d1b7f44-de9d-4f96-86d4-7713aa75e2e6</td>\n",
       "      <td>0.23295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9348c1b8-ccb5-4957-b77d-d00dfaefde5c</td>\n",
       "      <td>0.14337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>f4cbb06b-01a8-4a37-8d28-209c704d9c69</td>\n",
       "      <td>0.10999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6967f8ed-5236-48b2-afb9-a0695537f1c6</td>\n",
       "      <td>0.10185</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>128dd6c2-6a37-4bf7-901d-3b618f519c48</td>\n",
       "      <td>0.09966</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>dfdbe8e3-9d76-45a7-a138-29599749d40b</td>\n",
       "      <td>0.09824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0c6b50b4-2cdf-4509-a0c0-b9299e4ca303</td>\n",
       "      <td>0.09818</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  33bc32bc-2ffe-486f-9ee8-a86837911f50     1.00000\n",
       "1  de0d8858-0c64-4cba-af2b-86423ab5fad2     0.27114\n",
       "2  6f06efe2-95e8-4006-8ee6-73ca74671476     0.26880\n",
       "3  9d1b7f44-de9d-4f96-86d4-7713aa75e2e6     0.23295\n",
       "4  9348c1b8-ccb5-4957-b77d-d00dfaefde5c     0.14337\n",
       "5  f4cbb06b-01a8-4a37-8d28-209c704d9c69     0.10999\n",
       "6  6967f8ed-5236-48b2-afb9-a0695537f1c6     0.10185\n",
       "7  128dd6c2-6a37-4bf7-901d-3b618f519c48     0.09966\n",
       "8  dfdbe8e3-9d76-45a7-a138-29599749d40b     0.09824\n",
       "9  0c6b50b4-2cdf-4509-a0c0-b9299e4ca303     0.09818"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度数据\n",
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>33bc32bc-2ffe-486f-9ee8-a86837911f50</td>\n",
       "      <td>joy</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>de0d8858-0c64-4cba-af2b-86423ab5fad2</td>\n",
       "      <td>刘心如</td>\n",
       "      <td>0.27114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6f06efe2-95e8-4006-8ee6-73ca74671476</td>\n",
       "      <td>曾雯燕</td>\n",
       "      <td>0.26880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9d1b7f44-de9d-4f96-86d4-7713aa75e2e6</td>\n",
       "      <td>张铭睿</td>\n",
       "      <td>0.23295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9348c1b8-ccb5-4957-b77d-d00dfaefde5c</td>\n",
       "      <td>杨悦聪</td>\n",
       "      <td>0.14337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>f4cbb06b-01a8-4a37-8d28-209c704d9c69</td>\n",
       "      <td>洪可凡</td>\n",
       "      <td>0.10999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6967f8ed-5236-48b2-afb9-a0695537f1c6</td>\n",
       "      <td>周雨</td>\n",
       "      <td>0.10185</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>128dd6c2-6a37-4bf7-901d-3b618f519c48</td>\n",
       "      <td>刘瑜鹏</td>\n",
       "      <td>0.09966</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>dfdbe8e3-9d76-45a7-a138-29599749d40b</td>\n",
       "      <td>林嘉茵</td>\n",
       "      <td>0.09824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0c6b50b4-2cdf-4509-a0c0-b9299e4ca303</td>\n",
       "      <td>黄智毅</td>\n",
       "      <td>0.09818</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "0  33bc32bc-2ffe-486f-9ee8-a86837911f50      joy     1.00000\n",
       "6  de0d8858-0c64-4cba-af2b-86423ab5fad2      刘心如     0.27114\n",
       "2  6f06efe2-95e8-4006-8ee6-73ca74671476      曾雯燕     0.26880\n",
       "8  9d1b7f44-de9d-4f96-86d4-7713aa75e2e6      张铭睿     0.23295\n",
       "3  9348c1b8-ccb5-4957-b77d-d00dfaefde5c      杨悦聪     0.14337\n",
       "9  f4cbb06b-01a8-4a37-8d28-209c704d9c69      洪可凡     0.10999\n",
       "4  6967f8ed-5236-48b2-afb9-a0695537f1c6       周雨     0.10185\n",
       "5  128dd6c2-6a37-4bf7-901d-3b618f519c48      刘瑜鹏     0.09966\n",
       "1  dfdbe8e3-9d76-45a7-a138-29599749d40b      林嘉茵     0.09824\n",
       "7  0c6b50b4-2cdf-4509-a0c0-b9299e4ca303      黄智毅     0.09818"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合并\n",
    "pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5.删除人脸/人脸列表\n",
    "faceListId = \"list_004\" # 需要删除的人脸列表\n",
    "# 删除列表内人脸\n",
    "delete_face_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces/{}\"\n",
    "assert subscription_key\n",
    "# 获取上面的persistedFaceId\n",
    "persistedFaceId = r_add_face.json()[\"persistedFaceId\"] \n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 注意requests请求为delete\n",
    "r_delete_face = requests.delete(delete_face_url.format(faceListId,persistedFaceId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除人脸列表\n",
    "delete_facelist_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "r_delete_facelist = requests.delete(delete_facelist_url.format(faceListId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_facelist"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
